<?php

class CommentController
{
    private $commentid = 0;
    private $currentcommentid = 0;
    private $lbsModel;
    private $shopModel;
    private $topicModel;
    private $followModel;
    private $commentModel;
    private $autoUidModel;
    private $diaryListModel;
    private $diaryBodyModel;
    private $attachmentModel;
    private $topicCommentsModel;
    private $diaryAttachmentModel;
    private $diaryListCacheModel;
    private $tokenUserCacheModel;
    private $userStatisticsModel;
    private $childCommentModel;

    function __construct()
    {
        $this->lbsModel             =   new LbsModel();
        $this->shopModel            =   new ShopModel();
        $this->topicModel           =   new TopicModel();
        $this->followModel          =   new FollowModel();
        $this->commentModel         =   new CommentModel();
        $this->autoUidModel         =   new AutoUidModel();
        $this->diaryListModel       =   new DiaryListModel();
        $this->diaryBodyModel       =   new DiaryBodyModel();
        $this->attachmentModel      =   new AttachmentModel();
        $this->topicCommentsModel   =   new TopicCommentsModel();
        $this->diaryAttachmentModel =   new DiaryAttachmentModel();
        $this->diaryListCacheModel  =   new DiaryListCacheModel();
    	$this->tokenUserCacheModel  =   new TokenUserCacheModel();
    	$this->userStatisticsModel  =   new UserStatisticsModel();
        $this->childCommentModel        =   new ChildCommentModel();
    	$this->actions =   array(
    	    'commentDiary','getDiaryComments','addFavorOfDiaryComment','removeFavorOfDiaryComment','getHotDiaryComments','getChildComments','removeMyComment'
    	);
    }
    
    /**
     * 路由
     * @param unknown $param
     * @return unknown
     */
    public function callApi($param) {
        $ac = $param['action'];
        $doCheck    =   $this->checkAction($ac);
        if (!empty(AC::$errorNo)) {
            return  $doCheck;
        }
    
        return $this->$ac($param);
    }
    
    /**
     * 发布点滴评论
     * @param unknown $param
     * @return boolean|multitype:multitype:
     */
    public function commentDiary ($param) {
        $param = $this->filterPassedArgsOfCommentDrab($param);
        
        // 1.参数判断
        $argsCheck = $this->checkPassedArgsOfComment($param);
        if (empty($argsCheck)) return false;

        //检测是否被点滴/话题作者屏蔽
        $permissionService = new PermissionService();
        $shieldCheck = $permissionService->checkShieldUser($param['author_uid']);
        if (empty($shieldCheck)) return false;
        if(!empty($param['rUid']) && $param['rUid'] != $param['author_uid']) {
            //检测是否被被评论人屏蔽
            $shieldCheck = $permissionService->checkShieldUser($param['rUid']);
            if (empty($shieldCheck)) return false;
        }
        //提前释放检测类，以免被其继承的析构函数影响到报错信息
        $permissionService = NULL;

        // 2.数据组装(完善传参)
        $data = $this->finingCommentData($param);
        if (empty($data)) return false;

        $check = UK::checkVersion('5.50', '5.5.0');

        // 3.写评论表(没有父id或者是低版本则不适用楼中楼)
        if(intval($data['parentId']) == 0 || $check) {
            $commentId = $this->topicCommentsModel->commentDrab($data);
            if (empty($commentId)) return false;

            // 4.写附件表
            $this->addDrabAttachment($commentId, $data);
            // 5.组装返回评论信息
            $data['position'] = $commentId;
            $this->updateWeightOfComment($commentId, $param['tid'], $param['author_uid']);
            $comment= $this->commentModel->assembleDataOfComment($data, array(), $data['diary_create_time']);
        } else {
            if(empty($data['parentCommentInfo'])) {
                AC::$errorNo = 'COC008';
                AC::$errorMsg= '评论不存在或已被删除';
                return false;
            }
            $data['type'] = AC::COMMENT_TYPE_DIARY;
            $data['typeId'] = $data['tid'];
            $commentId = $this->childCommentModel->addComment($data);
            if (empty($commentId)) return false;

            //新评论更新父评论的被评论数，如果数量小于3 则要更新附加字段的信息
            if($commentId){
                $data['commentId'] = $commentId;
                $this->updateParentComment($param['parentId'], $param['tid'], $param['author_uid'], $data, $param['entrance']);
                //更新父评论的权重计数
                $this->updateWeightOfComment($data['parentId'], $param['tid'], $param['author_uid']);
            }

            $data['position'] = $data['parentId'];
            $data['positionId'] = $commentId;

            $comment = $this->childCommentModel->getCommentInfo(AC::COMMENT_TYPE_DIARY, $data['tid'], $commentId);
            $comment= $this->rebuildChildCommentData($comment, $param['author_uid']);
        }
        // 6.发动态、推送、反垃圾、更新相关计数或缓存数据
        $this->triggerRelatedOperationOfComment($data, $comment);

        if ($this->commentid && $comment['id']) $comment['id'] = $this->commentid;
        return $comment;
    }
    
    /**
     * 获取点滴评论列表
     * @param unknown $param
     * @return boolean|Ambigous <StdClass, multitype:number >
     */
    public function getDiaryComments ($param) {
        $data = $this->filterPassedArgsOfGetDiaryComments($param);
        
        // 1.参数判断
        $argsCheck = $this->checkPassedArgsOfGetComments($data);
        if (empty($argsCheck)) return false;
        
        // 2.获取点滴详情
        $diaryInfo  =   $this->diaryListModel->getDiaryInfoByBodyId($data['tid']);
        if (empty($diaryInfo['uid'])) {
            AC::$errorNo=   'DCC004';
            return  FALSE;
        }
        
        // 3.楼层跳转参数处理
        $data = $this->getStartByFloorNumber($data, $diaryInfo['commentTimes']);

        // 4.获取评论列表
        $comments = $this->getCommentsByDiaryTime($data, $diaryInfo);
        
        $result = $this->commentModel->getCommentsOfDrabs($comments, $diaryInfo['time']);
        return $result;
    }

    /**
     * 获取点滴热门评论列表
     * @param unknown $param
     * @return boolean|Ambigous <StdClass, multitype:number >
     */
    public function getHotDiaryComments ($param) {
        $data = $this->filterPassedArgsOfGetDiaryComments($param);
        
        // 1.参数判断
        $argsCheck = $this->checkPassedArgsOfGetComments($data);
        if (empty($argsCheck)) return false;
        
        // 2.获取点滴详情
        $diaryInfo  =   $this->diaryListModel->getDiaryInfoByBodyId($data['tid']);
        if (empty($diaryInfo['uid'])) {
            AC::$errorNo=   'DCC004';
            return  FALSE;
        }
        
        // 3.楼层跳转参数处理
        $data = $this->getStartByFloorNumber($data, $diaryInfo['commentTimes']);

        // 4.获取评论列表
        $commentIds = $this->diaryListCacheModel->getHotComment($data['tid'], $data['start'], $data['length']);
        if($commentIds){
            $comments = [];
            foreach ($commentIds as $key => $val) {
                if ($diaryInfo['time'] >= AC::$diaryCommentCf['transfer_time']) {
                    $commentInfo = $this->topicCommentsModel->getCommentInfo($val, $data['tid'], $diaryInfo['uid']);
                    if(empty($commentInfo)) continue;
                    $comments[] = $commentInfo; 
                }else{
                    $commentInfo = $this->commentModel->getOneComment(['uid'=>$diaryInfo['uid'], 'id'=>$val]);
                    if(empty($commentInfo)) continue;
                    $comments[] = $commentInfo; 
                }
            }
        }else{
            if ($diaryInfo['time'] >= AC::$diaryCommentCf['transfer_time']) {
                $comments = $this->topicCommentsModel->getDrabComments($data['tid'], $diaryInfo['uid'], $data['page'], $data['sort']);
            }else{
                $comments = $this->commentModel->getDiaryCommentList($diaryInfo['uid'], $data['tid'], $data['id'], $data['start'], $data['length']);
            }
        }
        
        $result = $this->commentModel->getCommentsOfDrabs($comments, $diaryInfo['time']);
        if(!$commentIds){
            if(isset($data['start']) && $data['start'] > 0){
                $start = ($data['start'] - 1) * $data['length'];
            }
            
            $result['comments'] = array_slice($result, $start , $data['length']);
            $result['counts'] = count($result['comments']);
        }
        return $result;
    }
    
    private function getCommentsByDiaryTime ($data, $diaryInfo) {
        if ($diaryInfo['time'] >= AC::$diaryCommentCf['transfer_time']) {
            $comments = $this->topicCommentsModel->getDrabComments($data['tid'], $diaryInfo['uid'], $data['page'], $data['sort']);
        }else{
            $comments = $this->commentModel->getDiaryCommentList($diaryInfo['uid'], $data['tid'], $data['id'], $data['start'], $data['length']);
        }
        
        return $comments;
    }
    
    /**
     * 点滴评论楼层点赞
     * @param unknown $param
     * @return boolean
     */
    public function addFavorOfDiaryComment ($param) {
        $data = $this->filterPassedArgsOfFavorComment($param);
        
        // 1.参数判断
        $argsCheck = $this->checkPassedArgsOfFavorComment($data);
        if (empty($argsCheck)) return false;
        
        // 2.数据组装(完善传参)
        $data = $this->finingFavorData($data);
        if (empty($data)) return false;

        // 3.逻辑判断
        if (empty($data['favoriteInfo'])) {
            $result = $this->topicModel->addFavorOfDiaryComment($data['uid'], $data['tid'], $data['position'], $data['author_uid'], $data['author_name']);
        }else{
            if ($data['favoriteInfo']['status']) {
                $result = $this->topicModel->updateFavorStatusOfDiaryComment($data['uid'], $data['tid'], $data['position'], $data['author_uid']);
            }else{
                AC::$errorNo = 'DCC008';
                return false;
            }
        }
        
        // 4.更新计数
        $this->topicModel->updateFavoriteOfDiaryComment($data['author_uid'], $data['tid'], $data['position']);
        $this->updateWeightOfComment($data['position'], $data['tid'], $data['author_uid']);
        return $result;
    }
    
    /**
     * 点滴楼层取消赞
     * @param unknown $param
     * @return boolean
     */
    public function removeFavorOfDiaryComment ($param) {
        $data = $this->filterPassedArgsOfFavorComment($param);
        
        // 1.参数判断
        $argsCheck = $this->checkPassedArgsOfFavorComment($data);
        if (empty($argsCheck)) return false;
        
        // 2.数据组装(完善传参)
        $data = $this->finingFavorData($data);
        if (empty($data)) return false;
        
        // 3.逻辑判断
        if ($data['favoriteInfo'] && empty($data['favoriteInfo']['status'])) {
            $result = $this->topicModel->removeFavorOfDiaryComment($data['uid'], $data['tid'], $data['position'], $data['author_uid']);
        }else{
            AC::$errorNo = 'DCC009';
            return false;
        }
        
        // 4.更新计数
        $this->topicModel->updateFavoriteOfDiaryComment($data['author_uid'], $data['tid'], $data['position'], 'del');
        $this->updateWeightOfComment($data['position'], $data['tid'], $data['author_uid']);
        return $result;
    }
    
    public function getCommentInfoList($commentInfo)/*{{{*/
    {
        if (empty($commentInfo['listMode']))
        {
            $result =   $this->commentModel->getCommentInfoList($commentInfo);
        }
        else
        {
            if ('up' == $commentInfo['listMode'])
            {
                $result =   $this->commentModel->getCommentInfoListUp($commentInfo);
            }
            else
            {
                $result =   $this->commentModel->getCommentInfoListDown($commentInfo);
            }
        }
        foreach ($result as &$value){
            $userInfo = $this->autoUidModel->getUserInfo($value['cUid']);
            $value['avatar']    =    $userInfo['avatar'];
            $value['user']      =    $this->autoUidModel->checkIsMe($userInfo);
            $value['user']['nickname']  =    $this->autoUidModel->getRemarkName($value['user']);

        }
        return  $result;
    }/*}}}*/

    public function getCommentReplyList($commentInfo)/*{{{*/
    {
        empty($commentInfo['length'])  &&  $commentInfo['length'] =   AC::COMMENT_LIST_DISPLAY_LEN;

        if (empty($commentInfo['listMode']))
        {
            $result =   $this->commentModel->getCommentReplyList($commentInfo);
        }
        else
        {
            if ('up' == $commentInfo['listMode'])
            {
                $result =   $this->commentModel->getCommentReplyListUp($commentInfo);
            }
            else
            {
                $result =   $this->commentModel->getCommentReplyListDown($commentInfo);
            }
        }
        foreach ($result as &$value){
            $userInfo = $this->autoUidModel->getUserInfo($value['cUid']);
            $value['avatar']    =    $userInfo['avatar'];
            $value['user']      =    $this->autoUidModel->checkIsMe($userInfo);
            $value['user']['nickname']  =    $this->autoUidModel->getRemarkName($value['user']);
        }
        return  $result;
    }/*}}}*/

    public function getCommentMeList($commentInfo)/*{{{*/
    {
        if (empty($commentInfo['listMode']))/*{{{*/
        {
            $result =   $this->commentModel->getCommentMeList($commentInfo);
        }
        else
        {
            if ('up' == $commentInfo['listMode'])
            {
                $result =   $this->commentModel->getCommentMeListUp($commentInfo);
            }
            else
            {
                $result =   $this->commentModel->getCommentMeListDown($commentInfo);
            }
        }
        if (!empty(AC::$errorNo))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
        }/*}}}*/

        if (empty($result)  ||  !is_array($result)) return  NULL;
        foreach ($result    as  &$info)
        {
            $info['detail'] =   $this->diaryListModel->getDiaryInfoByBodyId($info['bodyId']);
        }

        return  $result;
    }/*}}}*/

    public function getMeCommentList($commentInfo)/*{{{*/
    {
        if (empty($commentInfo['listMode']))/*{{{*/
        {
            $result =   $this->commentModel->getMeCommentList($commentInfo);
        }
        else
        {
            if ('up' == $commentInfo['listMode'])
            {
                $result =   $this->commentModel->getMeCommentListUp($commentInfo);
            }
            else
            {
                $result =   $this->commentModel->getMeCommentListDown($commentInfo);
            }
        }
        if (!empty(AC::$errorNo))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
        }/*}}}*/

        if (empty($result)  ||  !is_array($result)) return  NULL;
        foreach ($result    as  &$info)
        {
            $info['detail'] =   $this->diaryListModel->getDiaryInfoByBodyId($info['bodyId']);
            if (empty($info['detail'])){
                $info['detail'] = NULL;
            }
            // 添加用户字段
            $deltaUser      =   $this->autoUidModel->getUserInfo($info['cUid']);
            $info['detail']['user']  =   $this->autoUidModel->checkIsMe($deltaUser);
            $info['detail']['user']['nickname']  =    $this->autoUidModel->getRemarkName($info['detail']['user']);
        }

        return  $result;
    }/*}}}*/
    // 发表评论不返回评论结构
    public function postComment($commentInfo)/*{{{*/
    {
        $result = $this->addCommentInfo($commentInfo);
        if (!empty(AC::$errorNo)){
            return  FALSE;
        }
        if (isset($commentInfo['back']) && $commentInfo['back'] == 1){
            return $result;
        }
        return  TRUE;
    }/*}}}*/
    // 发表评论返回评论结构
    public function postReview($commentInfo)/*{{{*/
    {
        $result = $this->addCommentInfo($commentInfo);
        if (!empty(AC::$errorNo)){
            return  FALSE;
        }
        return $result;
    }/*}}}*/
    // 评论详情
    private function addCommentInfo($commentInfo){
        if (empty($commentInfo['uid'])  ||  empty($commentInfo['cUid']))
        {
            AC::$errorNo    =   'COC001';
            return  FALSE;
        }
        // 是否禁言
        $status  =  $this->autoUidModel->checkUserStatus(AC::$hostUserBaseInfo);
        if (!empty(AC::$errorNo)){
            return  FALSE;
        }
        #   access = FALSE  don't have premission to access this!
        $access =   $this->checkAccessPremission($commentInfo['cUid']);
        if (empty($access)) return  FALSE;
        

        $diaryInfo  =   $this->diaryListModel->getDiaryInfoByBodyId($commentInfo['bodyId']);
        if (empty($diaryInfo['uid'])) {
            AC::$errorNo=   'COC006';
            return  FALSE;
        }
        
        $userBaseInfo       =   $this->autoUidModel->getUserInfo($commentInfo['uid']);
        if (!isset($userBaseInfo['nickname']))
        {
            AC::$errorNo    =   'COC002';
            return  FALSE;
        }

        $cUidBaseInfo       =   $this->autoUidModel->getUserInfo($commentInfo['cUid']);
        if (!isset($cUidBaseInfo['nickname']))
        {
            AC::$errorNo    =   'COC003';
            return  FALSE;
        }

        $commentInfo['nickname']    =   $userBaseInfo['nickname'];
        $commentInfo['cNickname']   =   $cUidBaseInfo['nickname'];

        // 检查评论间隔
        $this->autoUidModel->checkUserSpace($commentInfo['uid']);
        if (!empty(AC::$errorNo)){
            return FALSE;
        }

        // 判断评论重复提交
//         $ck = $this->checkLastCommentContent($commentInfo);
//         if (AC::$errorNo){
//             UK::varDump(AC::$errorNo, __METHOD__);
//             return  FALSE;
//         }

        #   comment reply /*{{{*/
        if (!empty($commentInfo['parentId']))
        {
            if (empty($commentInfo['rUid']))
            {
                AC::$errorNo=   'COC004';
                return  FALSE;
            }
            $rUidBaseInfo   =   $this->autoUidModel->getUserInfo($commentInfo['rUid']);
            if (!isset($rUidBaseInfo['nickname']))
            {
                AC::$errorNo=   'COC005';
                return  FALSE;
            }
            $commentInfo['rNickname']   =   $rUidBaseInfo['nickname'];
        }
        else
        {
            $commentInfo['rUid']        =   0;
            $commentInfo['parentId']    =   0;
            $commentInfo['rNickname']   =   NULL;
        }/*}}}*/
        
        if ($diaryInfo['time'] >= AC::$diaryCommentCf['transfer_time']) {
            $result = false;
            $param = array(
                'uid'           =>  $commentInfo['uid'],
                'author_uid'    =>  $commentInfo['cUid'],
                'rUid'          =>  $commentInfo['rUid'],
                'tid'           =>  $commentInfo['bodyId'],
                'parentId'      =>  $commentInfo['parentId'],
                'type'          =>  $commentInfo['type'],
                'content'       =>  $commentInfo['content'],
                'repost'        =>  $commentInfo['repost'],
                'entrance'      =>  'addCommentInfo'
            );
        
            $diaryCommentInfo = $this->commentDiary($param);
            if ($diaryCommentInfo) {
                $param['id'] = $this->currentcommentid;
                $param['uid']       = $commentInfo['cUid'];
                $param['cUid']      = $commentInfo['uid'];
                $param['cNickname'] = $commentInfo['nickname'];
                $param['rNickname'] = $commentInfo['rNickname'];
                $param['avatar']    = $userBaseInfo['avatar'];
                $result = $this->CommnetInfo($param);
            }
            return $result;
        }
        
        if ($commentInfo['uid'] !=  $commentInfo['cUid'])
        {
            if (!empty($diaryInfo['secret']))
            {
                AC::$errorNo=   'COC007';
                return  FALSE;
            }
        }
        $quote = empty($diaryInfo['title']) ? $diaryInfo['abbreviation']:$diaryInfo['title'];
        if ($commentInfo['parentId'] > 0){
            $parentCommentInfo = $this->commentModel->getOneComment(array("uid"=>$diaryInfo['uid'],"id"=>$commentInfo['parentId']));
            if(empty($parentCommentInfo)){
                $commentInfo['rUid']        =   0;
                $commentInfo['parentId']    =   0;
                $commentInfo['rNickname']   =   NULL;
            }
            $quote = $parentCommentInfo['content'];
        }
        
        $result =   $this->commentModel->postComment($commentInfo);
        if (empty($result))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  FALSE;
        }
        $result['avatar']    =    $userBaseInfo['avatar'];

        $comment = $this->CommnetInfo($result);
        
         #  feed & statistics    /*{{{*/
        AC::$userStatistics[$commentInfo['uid']]['commentTimes']    =   1;
        AC::$userStatistics[$commentInfo['cUid']]['commentMeTimes'] =   1;
        if (empty($commentInfo['parentId']) &&  ($commentInfo['uid']    !=  $commentInfo['cUid']))
        {
            AC::$userStatistics[$commentInfo['cUid']]['newCommentTimes']=   1;
            AC::$userStatistics[$commentInfo['cUid']]['newLikeMeTimes'] =   1;
            AC::$userStatistics[$commentInfo['cUid']]['newCommentMeTimes'] =   1;
        }
        // 关于我的轮询计数
        if (!empty($commentInfo['rUid']) && ($commentInfo['uid']    !=  $commentInfo['rUid']) ){
            AC::$userStatistics[$commentInfo['rUid']]['newLikeMeTimes'] =   1;
            AC::$userStatistics[$commentInfo['rUid']]['newCommentMeTimes'] =   1;
        }
        if (isset(AC::$bodyidTypeClass[$commentInfo['type']]))
        {
            AC::$itemStatistics =   array(
            'commentTimes'      =>  1,
            'bodyidTypeClass'   =>  AC::$bodyidTypeClass[$commentInfo['type']],
            'uid'               =>  $commentInfo['cUid'],
            'bodyId'            =>  $commentInfo['bodyId'],
            'kitType'           =>  $commentInfo['type'],       # in Diary ,no kitType field!
            );
        }
        if (!empty($diaryInfo['secret']))   return  $comment;

        AC::$feedInfo   =   array(
            'uid'               =>  $commentInfo['uid'],
            'eUid'              =>  AC::$hostUserBaseInfo['uid'],
            'eNickname'         =>  AC::$hostUserBaseInfo['nickname'],
            'authorUid'         =>  $commentInfo['cUid'],
            'authorNickname'    =>  $commentInfo['cNickname'],
            'rUid'              =>  $commentInfo['rUid'],
            'rNickname'         =>  $commentInfo['rNickname'],
            'bodyId'            =>  $commentInfo['bodyId'],
            'parentId'          =>  $commentInfo['parentId'],
            'type'              =>  AC::FEED_COMMENT,
            'subType'           =>  $commentInfo['type'],
            'content'           =>  $commentInfo['content'],
            'quote'             =>  $quote,
        );/*}}}*/
        
        // 更新评论内容
//         $this->diaryListCacheModel->setLastCheckCommentConnent($commentInfo['uid'], $ck);

        $this->diaryListCacheModel->setCommentCheck($commentInfo['uid'], AC::SERVER_TIME);
        
        // 评论并转发
        if ($commentInfo['repost'] && empty(AC::$errorNo)) {
            $repost = array(
                'zfUid'     =>  $commentInfo['cUid'],
                'zfBodyId'  =>  $commentInfo['bodyId'],
                'uid'       =>  AC::HOST_UID,
                'content'   =>  $commentInfo['content']
            );
            $this->repostDiary($repost);
            AC::$errorNo = NULL;
        }
        
        # 异步反垃圾分析
        $classifyData = array(
                'uid'         =>    strval($commentInfo['uid']),
                'target'      =>    'diary_comment',
                'targetId'    =>    strval($commentInfo['bodyId']),
                'text'        =>    array(
                        'title'	    =>	'',
                        'content'	=>	strval($commentInfo['content'])
                ),
                'attachments' =>	array(
                        'images'	=>	array(),
                        'voices'	=>	array()
                ),
                'extra'       =>    array(
                        'commentId' =>    strval($comment['id']),
                        'cUid'	    =>    strval($commentInfo['cUid']),
                        'rUid'	    =>    strval($commentInfo['rUid']),
                        'bodyId'	=>    strval($commentInfo['bodyId']),
                        'parentId'	=>    strval($commentInfo['parentId'])
                ),
                'version'     =>    AC::$antispam_version
        );
	    
        $classifyDataOfJson = json_encode($classifyData);
        RedisPool::addClassifyGearmanJob("classify", $classifyDataOfJson);

		// 推送
        $push	=	new PushController();
        $push->push($commentInfo, "pushComment");
            
        return  $comment;
    }/*}}}*/
    
    public function repostDiary($diaryInfo) {
        $access =   $this->checkAccessPremission($diaryInfo['zfUid']);
        if ($access){
            $diaryInfo    =     $this->diaryListModel->repeatDataHandle($diaryInfo);
            if (!$diaryInfo){
                return false;
            }
        }
        
        $diaryId    =   $this->diaryBodyModel->addDiaryBody($diaryInfo);
        if (empty($diaryId)) return  FALSE;
    
        $diaryInfo['bodyId']=   $this->diaryBodyModel->getBodyId($diaryInfo['uid'], $diaryId);
        if (empty($diaryInfo['bodyId'])) return  FALSE;
        
        // LBS
        if (isset($diaryInfo['geo'])){
            $geo    = json_decode($diaryInfo['geo'],true);
            if (!empty($geo)){
                $geo['bodyId'] = $diaryInfo['bodyId'];
                $geo['uid']    = $diaryInfo['uid'];
                $this->lbsModel->addDiaryGeo($geo);
            }
        }
    
        $diaryListInsert=   $this->diaryListModel->addDiaryList($diaryInfo);
        if (empty($diaryListInsert)) return  FALSE;
    
        $result =   array('bodyId' => $diaryInfo['bodyId']);
        //将日志加入redis缓存
        $this->diaryListModel->getDiaryInfoByBodyId($diaryInfo['bodyId']);
        
        // 判断是否是转发
        $this->diaryListModel->repeatStatitcs($diaryInfo);
        $this->diaryListCacheModel->addUserTimeline($diaryInfo['uid'], $diaryInfo['bodyId']);
        
        // 添加到好友的关注列表
        $this->addDiaryToTimeLine($diaryInfo['uid'], $diaryInfo['bodyId']);
        
        // feedtime
        AC::$userStatistics[$diaryInfo['uid']]['diaryTimes']    =   1;

        return  $result;
    }/*}}}*/
    
    private function addDiaryToTimeLine ($uid, $bodyId) {
        if (empty($uid) || empty($bodyId)) return false;
        
        $jobdata = array(
            "uid" =>    $uid,
            "bodyId" => $bodyId
        );
        $jobkeyid = $uid."-".$bodyId;
        RedisPool::addGearmanJob("postdiary", $jobdata, $jobkeyid);
    }
    
    public function removeComment($commentInfo)/*{{{*/
    {
        $result =   $this->commentModel->removeComment($commentInfo);
        if (empty($result))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  FALSE;
        }
        if (empty($result['bodyId']))
        {
            AC::$userStatistics[$commentInfo['uid']]['commentTimes']    =   -1;
        }
        else
        {
            $this->diaryListModel->decCountTimes($result);
        }
        return  TRUE;
    }/*}}}*/
    
    # 批量删除评论    /*{{{*/
    public function removeManyComment($commenInfo)
    {
        $result =   $this->commentModel->removeManyComment($commenInfo);
        
        if (empty($result))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  FALSE;
        }
        
        if (empty($result['commentTimes']))
        {
            AC::$userStatistics[$commenInfo['uid']]['commentTimes']    =   -1;
        }
        else 
        {
            $this->diaryListModel->commentCountTimes($result);
        }
        
        return TRUE;
    }/*}}}*/
    
    private function getDetailInfo($info)/*{{{*/
    {
        $type   =   $info['type'];
        $detail =   UK::getDataByBodyid($type, $info['bodyId'], ' * ');

        if (empty($detail)) $detail =   NULL;
        if (empty($detail['attachmentPath'])  ||  empty($detail['uid']) ||  empty($detail['bodyId']))   return  $detail;
        $detail['attachmentList']   =   $this->diaryAttachmentModel->getDiaryAttachment($detail);
        return  $detail;
    }/*}}}*/

    private function checkAccessPremission($uid)/*{{{*/
    {
        if ((AC::HOST_UID   ==  $uid))  return  TRUE;

        $array      =   array('uid' => AC::HOST_UID, 'fUid' => $uid, 'alarm' => TRUE);
        $isBlackMe  =   $this->followModel->isBlackMe($array);
        if (empty($isBlackMe))  return  TRUE;

        return  FALSE;
    }/*}}}*/
    
    private function checkLastCommentContent($commentInfo){
        $ck = md5($commentInfo['content']);
        $lastCk = $this->diaryListCacheModel->getLastCheckCommentConnent($commentInfo['uid']);
        if($lastCk && $lastCk==$ck)
        {
            AC::$errorNo    =   'DC111';
            AC::$errorMsg   =   '日记评论内容重复提交';
            return  FALSE;
        }
        return $ck;
    }
    
    // 组装评论返回信息
    public function CommnetInfo($info){
        $commentInfo = array();
        $commentInfo['id']          =        strval($info['id']);
        $commentInfo['uid']         =        strval($info['uid']);
        $commentInfo['cUid']        =        strval($info['cUid']);
        $commentInfo['cNickname']   =        $info['cNickname']; 
        $commentInfo['avatar']      =        $info['avatar'];
        $commentInfo['rUid']        =        strval($info['rUid']);
        $commentInfo['rNickname']   =	     $this->autoUidModel->getRemarkName(array('uid'=>$info['rUid'],'nickname'=>$info['rNickname']));
        $commentInfo['bodyId']      =        strval($info['bodyId']);
        $commentInfo['parentId']    =        strval($info['parentId']);
        $commentInfo['type']        =        strval($info['type']);
        $commentInfo['content']     =        $info['content'];
        $commentInfo['commentType'] =        '1';
        $commentInfo['time']        =        strval(AC::SERVER_TIME);
        $commentInfo['replyTime']   =        strval(AC::SERVER_TIME);
        $commentInfo['replyNum']    =        '1';
        $commentInfo['status']      =        '0';
        
        return $commentInfo;
    }


    /**
     * 删除我的评论
     * @param $param
     * @return array|bool
     */
    public function removeMyComment($param) {
        $uid        = isset($param['uid'])          ? intval($param['uid'])         : 0;
        $tid        = isset($param['tid'])          ? intval($param['tid'])         : 0;
        $position   = isset($param['position'])     ? intval($param['position'])    : 0;
        $author_uid = isset($param['author_uid'])   ? intval($param['author_uid'])  : 0;
        $positionId = isset($param['positionId'])   ? intval($param['positionId'])  : 0;
        $host_uid   = AC::HOST_UID;
        //验证参数
        if($uid < 1 || $tid < 1 || $position < 1 || $author_uid < 1) {
            AC::$errorNo    =   'DCC001';
            return  false;
        }

        $userInfo = $this->autoUidModel->getUserInfo($uid);
        if(empty($userInfo) || $host_uid != $param['uid']) {
            AC::$errorNo    =   'DCC002';
            return  false;
        }
        //获取评论
        $commentInfo = $this->topicCommentsModel->getCommentInfo($position, $tid, $author_uid);
        //有$positionId则说明是子评论(楼中楼)
        if($positionId > 0) {
            $childComment = new ChildCommentModel();
            $childCommentInfo = $childComment->getCommentInfo(AC::COMMENT_TYPE_DIARY, $tid, $positionId);
            if(empty($commentInfo)){
                AC::$errorNo    =   'DCC009';
                AC::$errorMsg   =   '评论不存在';
                return  false;
            }
            $checkStatus = $this->checkDeleteOfComment($childCommentInfo, $uid);
            if(!$checkStatus) return false;
            //删除子评论
            $removeStatus = $childComment->removeChildComment(AC::COMMENT_TYPE_DIARY, $tid, $positionId);
            if(!$removeStatus){
                return false;
            }
            //更新父评论的附加字段
            $childCommentList = $childComment->getCommentsList(AC::COMMENT_TYPE_DIARY, $tid, $position);
            $this->updateParentCommentExtend($position, $tid, $author_uid, $childCommentList);
            //删除楼中楼评论返回主楼信息
            $commentInfo = $this->topicCommentsModel->getCommentInfo($position, $tid, $author_uid);
            $commentInfo = $this->commentModel->assembleDataOfComment($commentInfo, array(), AC::SERVER_TIME);
            return $commentInfo;
        }
        $this->checkDeleteOfComment($commentInfo, $uid);

        //删除评论
        $this->topicCommentsModel->removeTopicComment(['position' => $position, 'tid' => $tid, 'author_uid' => $author_uid]);
        if(AC::$errorNo ){
            return false;
        }
        //修改引用这条评论的评论
        $commentList = $this->topicCommentsModel->getCommentListByParentId($position, $tid, $author_uid);
        if($commentInfo) {
            $this->topicCommentsModel->updateComments($commentList);
        }
        if(AC::$errorNo ){
            return false;
        }
        return ['deleteStatus'  => true];
    }

    // ========================================== 评论的相关函数  =============================================
    /**
     * 过滤评论传参
     * @param unknown $param
     * @return multitype:number NULL unknown string
     */
    private function filterPassedArgsOfCommentDrab ($param) {
        $data = array(
            'uid'            =>  intval($param['uid']),
            'tid'            =>  intval($param['tid']),
            'author_uid'     =>  intval($param['author_uid']),
            'content'        =>  UK::getstr($param['content'], AC::$diaryCommentCf['content_limit']),
            'parentId'       =>  isset($param['parentId']) ? intval($param['parentId']) : 0,
            'type'           =>  isset($param['type']) ? intval($param['type']) : 1,
            'rUid'           =>  isset($param['rUid']) ? intval($param['rUid']) : 0,
            'extend'         =>  isset($param['extend']) ? strval($param['extend']) : '',
            'repost'         =>  intval($param['repost']),
            'attachmentList' =>  isset($param['attachment']) ? $param['attachment'] : array(),
            'voiceList'      =>  isset($param['voice']) ? $param['voice'] : array(),
            'entrance'       =>  isset($param['entrance']) ? $param['entrance'] : 'commentDiary'
        );
    
        $data['attachment'] = intval(count($data['attachmentList']) + count($data['voiceList']));
        if (strlen($data['content']) < 1 && $data['attachment']) {
            if ($data['attachmentList']) $data['content'] = '图片评论';
            if ($data['voiceList'])      $data['content'] = '语音评论';
            if ($data['attachmentList'] && $data['voiceList']) $data['content'] = '评论';
        }
        
        return $data;
    }
    
    /**
     * 传参判断
     * @param unknown $param
     * @return boolean
     */
    private function checkPassedArgsOfComment ($param) {
        $host_uid = AC::HOST_UID;
        // 判断传参
        if (empty($param['tid']) || empty($param['uid']) || empty($param['author_uid']) || strlen($param['content']) < 1) {
            AC::$errorNo    =   'DCC001';
            return  false;
        }
    
        // 非当前用户
        if (empty($host_uid) || $host_uid != $param['uid']) {
            AC::$errorNo    =   'DCC002';
            return  false;
        }
    
        return true;
    }

    /**
     * 删除评论前校验
     * @param $commentInfo
     * @param $uid
     * @return bool
     */
    private function checkDeleteOfComment($commentInfo, $uid) {
        if(empty($commentInfo)){
            AC::$errorNo    =   'DCC009';
            AC::$errorMsg   =   '评论不存在';
            return  false;
        }
        if($commentInfo['uid'] != $uid){
            AC::$errorNo    =   'DCC007';
            AC::$errorMsg   =   '无权删除此评论';
            return  false;
        }
        if($commentInfo['status'] != 0){
            AC::$errorNo    =   'DCC008';
            AC::$errorMsg   =   '该评论已被删除';
            return  false;
        }
        return true;
    }

    /**
     * 删除子评论时更新父评论附加字段
     * @param $position
     * @param $tid
     * @param $author_uid
     * @param $childCommentList
     * @return bool|int|resource
     */
    public function updateParentCommentExtend($position, $tid, $author_uid, $childCommentList) {
        $info = [
            'position'  =>  $position,
            'tid'       =>  $tid,
            'author_uid'=>  $author_uid
        ];
        if(empty($childCommentList)) {
            $info['extend'] = '';
            return $this->topicCommentsModel->updateTopicComment($info);
        } else {
            $count = count($childCommentList);
            $commentList = array_slice($childCommentList, 0, 3);
            foreach ($commentList as $value) {
                $extend['childComment'][] = [
                    "uid"           =>  $value['uid'],
                    'position'      =>  $value['parentId'],
                    'positionId'    =>  $value['position'],
                    'nickname'      =>  $value['nickname'],
                    "created_at"    =>  $value['created_at'],
                    "content"       =>  $value['content']
                ];
            }
            $extend['commentTimes'] = $count;
            $info['extend'] = UK::var_json_encode($extend);
            return $this->topicCommentsModel->updateTopicComment($info);
        }
    }

    /**
     * 完善传参
     * @param unknown $param
     * @return unknown
     */
    private function finingCommentData ($param) {
        // 获取用户信息，判断用户之间关系（是否拉黑）
        $userInfo = $this->autoUidModel->getUserInfo($param['uid']);
        if (empty($userInfo['uid']) || !isset($userInfo['nickname'])) {
            AC::$errorNo=   'DCC003';
            return false;
        }
    
        // 禁言限制
        $status  =  $this->autoUidModel->checkUserStatus($userInfo);
        if (AC::$errorNo) return false;
    
        // 被拉黑不能评论
        $access =   $this->checkAccessPremission($param['author_uid']);
        if (empty($access)) return  false;
        
        // 检查评论间隔
        $this->autoUidModel->checkUserSpace($param['uid']);
        if (!empty(AC::$errorNo)){
            return FALSE;
        }
    
        // 获取点滴详情
        $diaryInfo  =   $this->diaryListModel->getDiaryInfoByBodyId($param['tid']);
        if (empty($diaryInfo['uid']) || $diaryInfo['uid'] != $param['author_uid']) {
            AC::$errorNo=   'DCC004';
            return  false;
        }
    
        // 私密点滴非本人不能评论
        if ($diaryInfo['uid'] !=  AC::HOST_UID && $diaryInfo['secret']) {
            AC::$errorNo=   'DCC005';
            return  false;
        }
    
        // 作者信息
        $authorUserInfo = $this->autoUidModel->getUserInfo($param['author_uid']);
        if (empty($authorUserInfo['uid']) || !isset($authorUserInfo['nickname'])) {
            AC::$errorNo=   'DCC003';
            return false;
        }
    
        // 二级评论信息
        $quote          = strlen($diaryInfo['title']) < 1 ? $diaryInfo['abbreviation'] : $diaryInfo['title'];
        $parentComment  = $this->commentParentComment($param['parentId'], $param['tid'], $param['author_uid'], $param['rUid'], $diaryInfo['time'], $param['entrance']);
        if (!empty(AC::$errorNo)){
            return FALSE;
        }
//        $rUserInfo      = isset($parentComment['rUser']) ? $parentComment['rUser'] : array();
        $rUserInfo = $this->autoUidModel->getUserInfo($param['rUid']);
        unset($parentComment['rUser']);
        unset($parentComment['extend']);
        // 过滤表情信纸
        $shopIdsOfMine        = $this->filterShopData($param['uid'], $param['content']);
    
        $extend = array();
        if ($parentComment) $extend['parentComment']  = $parentComment;
        if ($shopIdsOfMine) $extend['shop_ids_mebuy'] = $shopIdsOfMine;
        $param['extend']      = UK::var_json_encode($extend);
        
        $topics               = $this->diaryListModel->getDiaryTopic($diaryInfo['topics']);
        $param['gid']         = $topics[0]['id'] ? $topics[0]['id'] : AC::$diaryCommentCf['diary_topicid'];
        $param['quote']       = $parentComment['content'] ? $parentComment['content'] : $quote;
        $param['nickname']    = $userInfo['nickname'];
        $param['author_uid']  = $diaryInfo['uid'];
        $param['author_name'] = $authorUserInfo['nickname'];
	    $param['parentId']    = $parentComment['parentId'] ? $parentComment['parentId'] : $param['parentId'];
        $param['rUid']        = isset($rUserInfo['uid']) ? $rUserInfo['uid'] : 0;
        $param['rNickname']   = isset($rUserInfo['nickname']) ? $rUserInfo['nickname'] : '';
        $param['new_diary']   = ($diaryInfo['time'] >= AC::$diaryCommentCf['transfer_time']) ? 1 : 0;
        $param['type']        = 1;
        $param['secret']      = $diaryInfo['secret'];
        $param['diary_create_time']        = $diaryInfo['time'];
        $param['parentCommentInfo']        = $parentComment;
        
        return $param;
    }
    
    /**
     * 评论的评论
     * @param unknown $parentId
     * @param unknown $tid
     * @param unknown $rUid
     * @return multitype:|boolean|Ambigous <multitype:, multitype:unknown string Ambigous <> >
     */
    private function commentParentComment ($parentId, $tid, $author_uid, $rUid, $time = 0, $entrance = '') {
        if (empty($parentId) || empty($tid) || empty($rUid) || empty($author_uid)) return array();
    
        $rUserInfo = $result = array();
        if ($time < AC::$diaryCommentCf['transfer_time'] || $entrance == 'addCommentInfo') {
            $parentComment = $this->commentModel->getOneComment(array('uid' => $author_uid, 'id' => $parentId));
            if ($parentComment) {
                $parentComment = array(
                    'uid'      =>  $parentComment['cUid'],
                    'position' =>   $parentId,
                    'time'     => $parentComment['time'],
                    'content'  => $parentComment['content'],
                    'parentId' => $parentComment['parentId']
                );
            }
        }else{
            $parentComment = $this->topicCommentsModel->getCommentInfo($parentId, $tid, $author_uid);
	       if ($parentComment) $parentComment['parentId'] = 0;
        }
        
        if ($parentComment) {
            if($parentComment['status']) {
                AC::$errorNo=   'DCC003';
                AC::$errorNo = 'COC008';
                AC::$errorMsg= '评论不存在或已被删除';
                return false;
            }
            if ($rUid != $parentComment['uid']) $rUid = $parentComment['uid'];
            $rUserInfo =  $this->autoUidModel->getUserInfo($rUid);
            if (empty($rUserInfo['uid']) || !isset($rUserInfo['nickname'])) {
                AC::$errorNo=   'DCC003';
                return false;
            }
    
            // 被拉黑不能评论
            $access =   $this->checkAccessPremission($rUid);
            if (empty($access)) return  false;
    
            $result = array(
                'uid'       => $parentComment['uid'],
                'position'  => $parentComment['position'],
                'time'      => $parentComment['time'],
                'parentId'  => isset($parentComment['parentId']) ? $parentComment['parentId'] : 0,
                'content'   => UK::getstr($parentComment['content'], 200,'',1),
                'extend'    => $parentComment['extend'],
                'rUser'     => $rUserInfo
            );
        }
    
        return $result;
    }

    /**
     * 获取楼中楼评论
     * @param $param
     * @return array|bool
     */
    public function getChildComments($param) {
        $data = $this->filterPassedArgsOfGetDiaryComments($param);

        // 1.参数判断
        $argsCheck = $this->checkPassedArgsOfGetComments($data);
        if (empty($argsCheck)) return false;

        // 2.获取点滴详情
        $diaryInfo  =   $this->diaryListModel->getDiaryInfoByBodyId($data['tid']);
        if (empty($diaryInfo['uid'])) {
            AC::$errorNo=   'DCC004';
            return  FALSE;
        }

        // 3.楼层跳转参数处理
        $data = $this->getStartByFloorNumber($data, $diaryInfo['commentTimes']);

        // 4.获取子评论列表
        $childComment = new ChildCommentModel();
        $comments = $childComment->getCommentsList(AC::COMMENT_TYPE_DIARY,$data['tid'], $data['position'], $data['positionId']);

        $result = $this->loopDataOfChildComment($comments, $diaryInfo['uid']);
        if(empty($result)) return (Object)array();
        $result = ['commentList' => $result, 'counts' => count($result)];
        return $result;
    }

    //循环格式化子评论数据
    private function loopDataOfChildComment($param, $author_uid){
        if (empty($param)) return array();

        $result = array();
        foreach ($param as $value) {
            if (empty($value['uid'])) continue;
            $childComment = $this->rebuildChildCommentData($value, $author_uid);
            $result[] = $childComment;
        }

        return $result;
    }

    /**
     * 组装子评论结构
     * @param $info
     * @param $author_uid
     * @return array
     */
    private function rebuildChildCommentData($info, $author_uid){
        if ($info['status'] > 0) $info['content'] = '该评论已被删除';
        $user = $this->autoUidModel->getUserInfo($info['uid']);
        $user['nickname'] = $this->autoUidModel->getRemarkName($user);
        $result = [
            "tid"           =>  $info['typeId'],
            "author_uid"    =>  $author_uid,
            "uid"           =>  $info['uid'],
            'position'      =>  $info['parentId'],             //父评论所在楼层
            'positionId'    =>  $info['position'],             //该评论的标示
            'nickname'      =>  $user['nickname'],
            "created_at"    =>  $info['created_at'],
            "content"       =>  $info['content'],
            "user"          =>  $user
        ];
        return $result;
    }

    /**
     * 添加子评论时更新父评论的附加字段
     * @param $parentId
     * @param $tid
     * @param $author_uid
     * @param $param
     * @param $entrance
     * @return bool
     */
    private function updateParentComment($parentId, $tid, $author_uid, $param, $entrance){
        if (empty($parentId) || empty($tid) || empty($author_uid) ) return false;
        if ($param['diary_create_time'] < AC::$diaryCommentCf['transfer_time'] || $entrance == 'addCommentInfo') {
            return true;
        }else{
            $parentComment = $this->topicCommentsModel->getCommentInfo($parentId, $tid, $author_uid);
            if ($parentComment) $parentComment['parentId'] = 0;
        }
        if(empty($parentComment)){
            return true;
        }

        $info = [
            'position'  =>  $parentId,
            'tid'       =>  $tid,
            'author_uid'=>  $author_uid
        ];
        $extend = UK::parseData($parentComment['extend']);
        if($extend['commentTimes'] >= 3) {
            $extend['commentTimes'] = $extend['commentTimes'] + 1;
        } else {
            $extend['childComment'][] = [
                "uid"           => $param['uid'],
                'position'      =>  $parentId,
                'positionId'    =>  $param['commentId'],
                'nickname'      =>  $param['nickname'],
                "created_at"    => AC::SERVER_TIME,
                "content"       => $param['content']
            ];
            $extend['commentTimes'] = $extend['commentTimes'] + 1;
        }

        $info['extend'] = UK::var_json_encode($extend);
        return $this->topicCommentsModel->updateTopicComment($info);
    }

    public function updateWeightOfComment($position, $bodyId, $authorUid) {
        $diaryInfo = $this->diaryListModel->getDiaryInfoByBodyId($bodyId);
        $comment = $this->topicCommentsModel->getCommentInfo($position, $bodyId, $authorUid);
        $commentInfo = $this->commentModel->assembleDataOfComment($comment, array(), $diaryInfo['time']);
        if($commentInfo['weight'] > 0) {
            $this->diaryListCacheModel->addHotComment($commentInfo['tid'], $commentInfo['weight'], $commentInfo['id']);
        } else {
            $this->diaryListCacheModel->delHotComment($commentInfo['tid'], $commentInfo['id']);
        }
        return $commentInfo;
    }

    
    /**
     * 过滤表情信纸等数据
     * @param unknown $uid
     * @param unknown $content
     * @return string
     */
    private function filterShopData ($uid, $content) {
        $result = array();
        if (empty($uid) || strlen($content) < 1) return $result;
    
        // 匹配日记内容，找出匹配到的全部表情的id列表
        $result = $paper_emotion_ids_mebuy = array();
        $matched_emotion_ids     = Emotion::matchEmotionIdList($content);
        $effective_ids           = $matched_emotion_ids;
        if (empty(AC::$iosFullVersion)) {
            if ($matched_emotion_ids){
                // 获取我已购买的全部信纸、表情
                $paper_emotion_ids_mebuy = $this->shopModel->getShopIdListMeBuy($uid);
            }
            $effective_ids       = array_intersect($paper_emotion_ids_mebuy, $matched_emotion_ids);
        }
    
        // 以json格式存储到数据库中
        if (is_array($effective_ids) && count($effective_ids) > 0){
            $result = $effective_ids;
        }
    
        return $result;
    }
    
    /**
     * 添加附件
     * @param unknown $commentId
     * @param unknown $data
     * @return boolean
     */
    private function addDrabAttachment ($commentId, $data) {
        if (AC::$errorNo) return false;
        $data['commentId'] = $commentId;
    
        // 图片附件
        if (is_array($data['attachmentList'])  &&  count($data['attachmentList']) > 0 ) {
            $imgInfo    =   array(
                'tid'        =>  $data['tid'],
                'uid'        =>  $data['author_uid'],
                'pid'        =>	 $commentId,
                'attachment' =>  $data['attachmentList'],
                'type'       =>  AC::IMGATTACHMENTTYPE
            );
    
            $this->attachmentModel->addAttachment($imgInfo);
        }
    
        // 语音附件
        if (is_array($data['voiceList'])  &&  count($data['voiceList']) > 0) {
            $voiceInfo    =   array(
                'tid'        =>  $data['tid'],
                'uid'        =>  $data['author_uid'],
                'pid'        =>	 $commentId,
                'attachment' =>  $data['voiceList'],
                'type'       =>  AC::VOICEATTACHMENTTYPE
            );
            $this->attachmentModel->addAttachment($voiceInfo);
        }
    
        AC::$errorNo = '';
        return true;
    }
    
    /**
     * 触发相关操作
     * @param unknown $data
     * @return boolean
     */
    private function triggerRelatedOperationOfComment ($data, $comment) {
        if (AC::$errorNo) return false;
    
        // 转发、发动态、推送、反垃圾、更新相关计数或缓存数据
        $this->repostDrab($data);
        $this->postDrabCommentFeed($data);
        $this->postDiaryComment($data, $comment);
        $this->pushDrabComment($data);
        $this->sendCommentToClassify($data);
        $this->diaryListCacheModel->setCommentCheck($data['uid'], AC::SERVER_TIME);
        $this->topicModel->updateTimeOfDiarybyTopic($data['tid'], $data['gid']);
        
        AC::$errorNo = '';
        return true;
    }
    
    /**
     * 转发
     * @param unknown $info
     * @return boolean
     */
    private function repostDrab($info) {
        if ($info['repost']) {
            $repost = array(
                'zfUid'     =>  $info['author_uid'],
                'zfBodyId'  =>  $info['tid'],
                'uid'       =>  $info['uid'],
                'content'   =>  $info['content']
            );
            $this->repostDiary($repost);
        }
        return true;
    }
    
    /**
     * feed更新
     * @param unknown $info
     * @return unknown
     */
    private function postDrabCommentFeed ($info) {
        AC::$userStatistics[$info['uid']]['commentTimes']           =   1;
        AC::$userStatistics[$info['author_uid']]['commentMeTimes']  =   1;
        if (empty($info['parentId']) &&  ($info['uid'] !=  $info['author_uid'])) {
            AC::$userStatistics[$info['author_uid']]['newCommentTimes']   =   1;
            AC::$userStatistics[$info['author_uid']]['newLikeMeTimes']    =   1;
            AC::$userStatistics[$info['author_uid']]['newCommentMeTimes'] =   1;
        }
    
        if (!empty($info['rUid']) && ($info['uid']    !=  $info['rUid']) ){
            AC::$userStatistics[$info['rUid']]['newLikeMeTimes']    =   1;
            AC::$userStatistics[$info['rUid']]['newCommentMeTimes'] =   1;
        }

        $check = UK::checkVersion('5.50', '5.5.0');

        if (isset(AC::$bodyidTypeClass[$info['type']]) && ($check || intval($info['parentId'])  == 0)) {
            $set = [
                'uid' => $info['author_uid'],
                'bodyId' => $info['tid'],
                'commentTimes' => $info['position']
            ];
            $this->diaryListModel->setDiaryList($set);
            $this->diaryListCacheModel->delDiaryCache($set['bodyId']);
            /*AC::$itemStatistics =   array(
                'commentTimes'      =>  1,
                'bodyidTypeClass'   =>  AC::$bodyidTypeClass[$info['type']],
                'uid'               =>  $info['author_uid'],
                'bodyId'            =>  $info['tid'],
                'kitType'           =>  $info['type']
            );*/
        }
        if (!empty($info['secret']))   return  true;
    
        AC::$feedInfo   =   array(
            'uid'               =>  $info['uid'],
            'eUid'              =>  $info['uid'],
            'eNickname'         =>  $info['nickname'],
            'authorUid'         =>  $info['author_uid'],
            'authorNickname'    =>  $info['author_name'],
            'rUid'              =>  $info['rUid'],
            'rNickname'         =>  $info['rNickname'],
            'bodyId'            =>  $info['tid'],
            'parentId'          =>  $info['parentId'],
            'position'          =>  $info['position'],
            'type'              =>  AC::FEED_COMMENT,
            'subType'           =>  $info['type'],
            'content'           =>  $info['content'],
            'quote'             =>  $info['quote']
        );

    }
    
    /**
     * 评论日记
     * @param unknown $info
     * @param unknown $comment
     * @return boolean
     */
    private function postDiaryComment ($info, $comment) {
        if (AC::$errorNo) return false;
        
        $data = array(
            'uid'           =>  $info['uid'],
            'nickname'      =>  $info['nickname'],
            'cUid'          =>  $info['author_uid'],
            'cNickname'     =>  $info['author_name'],
            'rUid'          =>  $info['rUid'],
            'rNickname'     =>  $info['rNickname'],
            'bodyId'        =>  $info['tid'],
            'parentId'      =>  ($info['diary_create_time'] >= AC::$diaryCommentCf['transfer_time'] && $info['entrance'] === 'commentDiary') ? 0 : $info['parentId'],
            'type'          =>  $info['type'],
            'content'       =>  $info['content'],
            'extra'         =>  UK::var_json_encode($comment)
        );

        $result = $this->commentModel->addComment($data);
        $this->currentcommentid = $result;
        if ($result && $info['diary_create_time'] < AC::$diaryCommentCf['transfer_time']) $this->commentid = $result;
        
        AC::$errorNo = '';
        return true;
    }
    
    /**
     * 推送
     * @param unknown $info
     */
    private function pushDrabComment ($info) {
        $info['cUid'] = $info['author_uid'];
        $push	=	new PushController();
        $push->push($info, "pushComment");
    }
    
    /**
     * 反垃圾收集
     * @param unknown $info
     */
    private function sendCommentToClassify ($info) {


        $classifyData = array(
            'uid'         =>    strval($info['uid']),
            'target'      =>    'drab_comment',
            'targetId'    =>    strval($info['tid']),
            'text'        =>    array(
                'title'	      =>	'',
                'content'	  =>	strval($info['content'])
            ),
            'attachments' =>	array(
                'images'	=>	$info['attachmentList'],
                'voices'	=>	$info['voiceList']
            ),
            'extra'       =>    array(
                'gid'           =>    strval($info['gid']),
                'position'	    =>    strval($info['position']),
                'author_uid'    =>    strval($info['author_uid']),
                'rUid'          =>    strval($info['rUid']),
                'parentId'      =>    strval($info['parentId']),
                'positionId'      =>    isset($info['positionId']) ? strval($info['positionId']) : '0',
            ),
            'version'     =>    AC::$antispam_version
        );
        $classifyDataOfJson = json_encode($classifyData);
        RedisPool::addClassifyGearmanJob("classify", $classifyDataOfJson);
    }
    
    private function filterPassedArgsOfFavorComment ($param) {
        $data = array(
            'uid'       =>  AC::HOST_UID,
            'tid'       =>  intval($param['tid']),
            'position'  =>  intval($param['position']),
            'author_uid'=>  intval($param['author_uid'])
        );
    
        return $data;
    }
    
    private function checkPassedArgsOfFavorComment ($param) {
        $host_uid = AC::HOST_UID;
        // 判断传参
        if (empty($param['tid']) || empty($param['uid']) || empty($param['position']) || empty($param['author_uid'])) {
            AC::$errorNo    =   'DCC001';
            return  false;
        }
    
        // 非当前用户
        if (empty($host_uid) || $host_uid != $param['uid']) {
            AC::$errorNo    =   'DCC002';
            return  false;
        }
    
        return true;
    }
    
    /**
     * 完善点赞传参数据
     * @param unknown $param
     * @return boolean|unknown
     */
    private function finingFavorData ($param) {
        $diaryInfo = $this->diaryListModel->getDiaryInfoByBodyId($param['tid']);
        if (empty($diaryInfo['uid'])) {
            AC::$errorNo=   'LC004';
            return  false;
        }
        if (!empty($diaryInfo['secret'])) {
            AC::$errorNo=   'LC005';
            return  false;
        }
        
        $param['author_uid'] = $diaryInfo['uid'];
        $commentInfo = $this->topicCommentsModel->getCommentUid($param);
        if (empty($commentInfo)) {
            AC::$errorNo    =   'DCC006';
            return FALSE;
        }
    
        // 被拉黑不能收藏
        $access =   $this->checkAccessPremission($param['author_uid']);
        if (empty($access)) return  false;
    
        // 获取点滴作者信息
        $authorInfo    =    $this->autoUidModel->getUserInfo($param['author_uid']);
        if (!isset($authorInfo['nickname'])){
            AC::$errorNo    =   'DCC003';
            return FALSE;
        }
        
        // 获取点赞情况
        $favoriteInfo    =    $this->topicModel->getFavorInfoOfDiaryComment($param['uid'], $param['tid'], $param['position']);

        // 组装数据
        $param['author_name'] = UK::getstr($authorInfo['nickname'], 24, '', 1);
        $param['favoriteInfo']= $favoriteInfo;
        
        return $param;
    }
    
    // ======================================== 获取评论列表相关函数  ================================================
    
    /**
     * 过滤传参
     * @param unknown $param
     * @return multitype:number NULL unknown string
     */
    private function filterPassedArgsOfGetDiaryComments ($param) {
        list($id, $start, $length) = UK::assemblePageData($param);
        
        $data = array(
            'uid'       =>  AC::HOST_UID,
            'tid'       =>  intval($param['tid']),
            'parentId'  =>  intval($param['parentId']),
            'id'        =>  $id,
            'start'     =>  $start,
            'length'    =>  $length,
            'sort'      =>  (isset($param['sort']) && in_array($param['sort'], array('all', 'hot', 'author', 'new'))) ? $param['sort'] : 'all',
            'position'  =>  (isset($param['position']) && intval($param['position']) > 0) ? intval($param['position']) : 0,
            'positionId'=>  (isset($param['positionId']) && intval($param['positionId']) > 0) ? intval($param['positionId']) : 0
        );

        return $data;
    }
    
    /**
     * 楼层跳转
     * @param unknown $param
     * @param unknown $commentTimes
     * @return number
     */
    private function getStartByFloorNumber ($param, $commentTimes) {
        if ($param['position'] > 0 && $param['sort'] === 'all') {
            $page  = ceil($param['position'] / $param['length']) > 1 ? ceil($param['position'] / $param['length']) - 1 : 0;
            $param['id'] = intval($page * $param['length']);
        }
        
        $param['page'] = UK::assemblePageData($param);
        return $param;
    }
    
    /**
     * 判断参数是否合法
     * @param unknown $param
     * @return boolean
     */
    private function checkPassedArgsOfGetComments ($param) {
        $host_uid = AC::HOST_UID;
        // 判断传参
        if (empty($param['tid']) || empty($param['uid'])) {
            AC::$errorNo    =   'DCC001';
            return  false;
        }
    
        // 非当前用户
        if (empty($host_uid) || $host_uid != $param['uid']) {
            AC::$errorNo    =   'DCC002';
            return  false;
        }
    
        return true;
    }
    private function checkAction($action){
    
        if (in_array($action, $this->actions) && method_exists($this, $action))  return  TRUE;
        AC::$errorNo    =   'SA004';
        return  ' Error : call action is not exists in CommentController! ';
    }
}

